package com.yiyuzi.admin.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiyuzi.admin.common.constant.ResponseCodeEnum;
import com.yiyuzi.admin.common.domain.PageResultDTO;
import com.yiyuzi.admin.common.utils.ConvertUtils;
import com.yiyuzi.admin.role.domain.entity.RoleEntity;
import com.yiyuzi.admin.role.service.RoleService;
import com.yiyuzi.admin.user.UserRoleDao;
import com.yiyuzi.admin.user.domain.dto.UserRoleAddDTO;
import com.yiyuzi.admin.user.domain.dto.UserRoleDTO;
import com.yiyuzi.admin.user.domain.dto.UserRoleQueryDTO;
import com.yiyuzi.admin.user.domain.entity.UserEntity;
import com.yiyuzi.admin.user.domain.entity.UserRoleEntity;
import com.yiyuzi.admin.user.service.UserRoleService;
import com.yiyuzi.admin.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yiyuzi
 * @date 2021/5/8 9:43
 */

@Service
@Transactional
public class UserRoleServiceImpl extends ServiceImpl<UserRoleDao, UserRoleEntity> implements UserRoleService {
    @Resource
    private UserRoleDao userRoleDao;

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @Override
    public UserRoleDTO findById(Long id) {
        UserRoleEntity entity = userRoleDao.selectById(id);
        if(entity == null)
            return null;
        UserRoleDTO userRoleDTO = ConvertUtils.copy(entity, UserRoleDTO.class);

        UserEntity userEntity = userService.getById(entity.getUserId());
        RoleEntity roleEntity = roleService.getById(entity.getRoleId());
        userRoleDTO.setRoleName(roleEntity.getRoleName());
        userRoleDTO.setLoginName(userEntity.getLoginName());
        userRoleDTO.setActualName(userEntity.getActualName());

        return userRoleDTO;
    }

    @Override
    public UserRoleDTO findOne(UserRoleQueryDTO queryDTO) {
        UserRoleEntity entity = this.getOne(new QueryWrapper<UserRoleEntity>()
                .eq(queryDTO.getId() != null,"id", queryDTO.getId())
                .eq(queryDTO.getUserId() != null,"user_id", queryDTO.getUserId())
                .eq(queryDTO.getRoleId() != null,"role_id", queryDTO.getRoleId()));
        return this.findById(entity.getId());
    }

    @Override
    public PageResultDTO<UserRoleDTO> findPage(UserRoleQueryDTO queryDTO) {
        List<UserRoleEntity> list = this.list(new QueryWrapper<UserRoleEntity>()
                .eq(queryDTO.getId() != null,"id", queryDTO.getId())
                .eq(queryDTO.getUserId() != null,"user_id", queryDTO.getUserId())
                .eq(queryDTO.getRoleId() != null,"role_id", queryDTO.getRoleId()));
        List<UserRoleDTO> userRoleDTOList = new ArrayList<>();
        for (UserRoleEntity entity : list){
            userRoleDTOList.add(this.findById(entity.getId()));
        }
        return ConvertUtils.convertPageResult(userRoleDTOList, UserRoleDTO.class);
    }

    @Override
    public boolean add(UserRoleAddDTO addDTO) {
        if(addDTO.getRoleId() == null || addDTO.getUserId() == null)
            return false;
        if(this.getOne(new QueryWrapper<UserRoleEntity>()
                .eq(addDTO.getUserId() != null,"user_id", addDTO.getUserId())
                .eq(addDTO.getRoleId() != null,"role_id", addDTO.getRoleId())) != null)
            return false;
        if(userService.getById(addDTO.getUserId()) == null)
            return false;
        if(roleService.getById(addDTO.getRoleId()) == null)
            return false;
        return this.save(ConvertUtils.copy(addDTO, UserRoleEntity.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(List<UserRoleAddDTO> list) {
        for(UserRoleAddDTO addDTO : list){
            if (!this.add(addDTO)){
                throw new RuntimeException(ResponseCodeEnum.FAIL.getDesc());
            }
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteList(List<Long> list) {
        for(Long id: list) {
            if(!this.removeById(id))
                throw new RuntimeException(ResponseCodeEnum.FAIL.getDesc());
        }
        return true;
    }
}
